//
//  ChatCustomCell.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/26.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "ChatCustomCell.h"
#import "Common.h"
#import "ChatCustom.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"
@interface ChatCustomCell ()

//@property (nonatomic,strong)UIImageView *headImage;
//@property (nonatomic,strong)UIImageView *bgTxtImage;


@property (nonatomic,strong)UIButton *iconImage;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *txtLabel;
@property (nonatomic,strong)UILabel *iconLabel;
@property (nonatomic,strong)NIMMessage *message;

@end


@implementation ChatCustomCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
 
        
        UIView *bgview = [[UIView alloc]initWithFrame:CGRectMake(55, 20, SCREENW-130, 150)];
        bgview.backgroundColor = [UIColor whiteColor];
        bgview.layer.masksToBounds = YES;
        bgview.layer.cornerRadius = 8;
        [self addSubview:bgview];
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, bgview.frame.size.width-40, 30)];
        self.titleLabel.textColor = bgmmainColor;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [bgview addSubview:self.titleLabel];
        
        self.txtLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, self.titleLabel.frame.size.width, 65)];
        self.txtLabel.numberOfLines = 0;
        self.txtLabel.textColor = bgmgrayColor;
        self.txtLabel.font = [UIFont systemFontOfSize:14];
        [bgview addSubview:self.txtLabel];
        
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, 110, bgview.frame.size.width, 0.3)];
        line.backgroundColor = [UIColor grayColor];
        line.alpha = 0.3;
        [bgview addSubview:line];

        self.iconImage = [[UIButton alloc]initWithFrame:CGRectMake(20, line.frame.origin.y+8, 25, 25)];
        [bgview addSubview:self.iconImage];
        
        self.iconLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, line.frame.origin.y, 80, 40)];
        self.iconLabel.textColor = bgmgrayColor;
        self.iconLabel.font = [UIFont systemFontOfSize:14];
        [bgview addSubview:self.iconLabel];
        
        
        bgview.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPushMsg)];
        [bgview addGestureRecognizer:tap];
        
    }
    return self;
}


- (void)setCustomMsg:(NIMMessage *)message{
    
    self.message = message;
 
    NIMCustomObject *cusstomObject =message.messageObject;
    ChatCustom *pushMsg = cusstomObject.attachment;
    if(pushMsg == nil){
        return;
    }

    self.titleLabel.text = [NSString stringWithFormat:@"%@",pushMsg.pushTitle];
    self.txtLabel.text = [NSString stringWithFormat:@"%@",pushMsg.pushTxt];
    NSString *pushType = pushMsg.pushType;
    NSString *imageStr = [NSString stringWithFormat:@"push_%@",pushType];
    [self.iconImage setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    int ptyId = [pushType intValue];
    
    if ([pushType intValue]>8) {
        self.iconLabel.text = [publicTypeArr objectAtIndex:ptyId-1];
    }else{
        
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        NSArray *typeArr = [ud objectForKey:@"PUSHTYPE"];
        NSDictionary *typedic = [typeArr objectAtIndex:(ptyId-1)];
        self.iconLabel.text = [typedic objectForKey:@"name"];
        
    }
 
 
    
    
    UIFont * tfont = [UIFont systemFontOfSize:14];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize =[self.txtLabel.text boundingRectWithSize:CGSizeMake(self.titleLabel.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    
    if(actualsize.height>65){
        self.txtLabel.frame = CGRectMake(20, 40, self.titleLabel.frame.size.width, 65);

    }else{
        self.txtLabel.frame = CGRectMake(20, 40, self.titleLabel.frame.size.width, actualsize.height);

    }
}





- (void)clickPushMsg{
    [self.customDelegate gotoPushInfo:self.message];
}

@end
